home *** CD-ROM | disk | FTP | other *** search
/ The Business Master (3rd Edition) / The Business Master (3rd Edition).iso / files / business / stata2 / graph.kit < prev    next >
Encoding:
Text File  |  1988-08-21  |  8.6 KB  |  394 lines

  1. set output error
  2.  
  3. display _new in blue /*
  4. */ "Loading Graph.Kit Release 2.0  Demonstration Version" _n /*
  5. */ "Copyright (c) 1987-1988 by ==C=R=C==.  All rights reserved."
  6.  
  7.  
  8.  
  9. /*
  10.   User's wishing to edit Graph.Kit please see comments at end of this file.
  11. */
  12.  
  13.  
  14. /*
  15.     utility routines used by qqplot
  16. */
  17.  
  18. capture program drop _qqalign
  19. program define _qqalign
  20.     capture {
  21.         sort %_1
  22.         gen _YORDER=_n
  23.         sort %_2
  24.         gen %_3=%_2[_YORDER]
  25.         drop _YORDER
  26.         exit
  27.     }
  28.     capture drop _YORDER
  29.     BREAK
  30. end
  31.  
  32. * _qqcomp varname old# new#
  33.  
  34. capture program drop _qqcomp
  35. program define _qqcomp
  36.     capture {
  37.         sort %_1
  38.         gen int _INT=(_n-0.5)*%_2/%_3+0.5
  39.         gen _FRAC=0.5+(_n-0.5)*%_2/%_3-_INT
  40.         gen _TEMP=(1-_FRAC)*%_1[_INT]+_FRAC*%_1[_INT+1]
  41.         replace %_1=_TEMP
  42.         drop _INT _FRAC _TEMP
  43.         exit
  44.     }
  45.     capture drop _INT
  46.     capture drop _FRAC
  47.     capture drop _TEMP
  48.     BREAK
  49. end
  50.  
  51. * utility routines
  52.  
  53. capture program drop _setlbl
  54. program define _setlbl
  55.     mac def _w : variable label %_2
  56.     if "%_w"=="" {
  57.         mac def _w "%_2"
  58.     }
  59.     label variable %_1 "%_w"
  60. end
  61.  
  62. capture program drop _nmusig2
  63. program define _nmusig2
  64.     capture {
  65.         sum %_1 %_5 %_6
  66.         gen int %_2 = _result(1) in l
  67.         gen double %_3 = _result(3) in l
  68.         gen double %_4 = _result(4) in l
  69.         exit
  70.     }
  71.     capture drop %_2
  72.     capture drop %_3
  73.     capture drop %_4
  74.     BREAK
  75. end
  76.  
  77. capture program drop _chkstr
  78. program define _chkstr
  79.     mac def _if "opt"
  80.     noisily parse "%_*"
  81. end
  82.  
  83.  
  84. * new commands
  85.  
  86. capture program drop symplot
  87. program define symplot
  88.     if "%_*"=="" {
  89.         di in bl "-> symplot varname [if] [in] [, graph_options]"
  90.         exit
  91.     }
  92.     mac def _varlist "req ex max(1)"
  93.     mac def _if "opt"
  94.     mac def _in "opt"
  95.     mac def _options "*"
  96.     parse "%_*"
  97.     capture {
  98.         gen double _VAR = %_varlist %_if %_in
  99.         sort _VAR
  100.         gen int _CNT=sum(_VAR~=.)
  101.         if _CNT[_N]==0 {
  102.             drop _VAR _CNT
  103.             di in red "no observations"
  104.             exit
  105.         }
  106.         mac def _midpt = int(_CNT[_N]+1)/2
  107.         #delimit ;
  108.         mac def _median=cond(int(_CNT[_N]/2)*2==_CNT[_N],
  109.                 (_VAR[%_midpt]+_VAR[%_midpt+1]) / 2,
  110.                 _VAR[%_midpt]) ;
  111.         #delimit cr
  112.         gen _BELOW=_VAR-(%_median)
  113.         drop _VAR
  114.         sort _BELOW
  115.         gen _ABOVE=_BELOW[_CNT[_N]+1-_n]
  116.         replace _BELOW=abs(_BELOW)
  117.         gen _LINE=_BELOW
  118.         lab var _LINE " "
  119.         lab var _BELOW "Distance below median"
  120.         lab var _ABOVE "Distance above median"
  121.         mac def _w : variable label %_varlist
  122.         if "%_w"=="" {
  123.             mac def _w "%_varlist"
  124.         }
  125.         graph _ABOVE _LINE _BELOW if _n<=%_midpt, /*
  126.               */    sy(oi) c(.l) sort %_options t2("%_w")
  127.     }
  128.     if _rc {
  129.         di in red "--Break--"
  130.     }
  131.     capture drop _VAR
  132.     capture drop _ABOVE
  133.     capture drop _BELOW
  134.     capture drop _LINE
  135.     capture drop _CNT
  136. end
  137.  
  138.  
  139. capture program drop quantile
  140. program define quantile
  141.     if "%_*"=="" {
  142.         di in bl "-> quantile varname [if] [in] [, graph_options]"
  143.         exit
  144.     }
  145.     mac def _varlist "req ex max(1)"
  146.     mac def _if "opt"
  147.     mac def _in "opt"
  148.     mac def _options "*"
  149.     parse "%_*"
  150.     capture {
  151.         gen _QUANT=%_varlist %_if %_in
  152.         sort _QUANT
  153.         gen int _CNT=sum(_QUANT~=.)
  154.         gen _FRAC=(_CNT-0.5)/_CNT[_N]
  155.         gen _LINE=_QUANT[1]+_CNT*((_QUANT[_CNT[_N]]-_QUANT[1])/_CNT[_N])
  156.         _setlbl _QUANT %_varlist
  157.         mac def _w : variable label _QUANT
  158.         lab var _QUANT "Quantiles of %_w"
  159.         lab var _FRAC "Fraction of the data"
  160.         lab var _LINE " "
  161.         #delimit ;
  162.         graph _QUANT _LINE _FRAC, s(oi) c(.l) xsca(0,1)
  163.               %_options xlab(0,.25,.5,.75,1) ;
  164.         #delimit cr
  165.     }
  166.     if _rc {
  167.         di in red "--Break--"
  168.     }
  169.     capture drop _QUANT
  170.     capture drop _LINE
  171.     capture drop _FRAC
  172.     capture drop _CNT
  173. end
  174.  
  175. capture program drop qqplot
  176. program define qqplot
  177.     if "%_*"=="" {
  178.         di in bl "-> qqplot yvar xvar [if] [in] [, graph_options]"
  179.         exit
  180.     }
  181.     mac def _varlist "req ex min(2) max(2)"
  182.     mac def _if "opt"
  183.     mac def _in "opt"
  184.     mac def _options "*"
  185.     parse "%_*"
  186.     parse "%_varlist", parse(" ")
  187.     capture {
  188.         gen _VARY=%_1 %_if %_in
  189.         gen _VARX=%_2 %_if %_in
  190.         gen int _CNT=sum(_VARY~=.)
  191.         mac def _cnty=_CNT[_N]
  192.         replace _CNT=sum(_VARX~=.)
  193.         mac def _cntx=_CNT[_N]
  194.         drop _CNT
  195.         if %_cntx==0 | %_cnty==0 {
  196.             drop _VARY _VARX
  197.             di in red "no observations"
  198.             exit
  199.         }
  200.         if %_cnty>%_cntx {
  201.             _qqcomp _VARY %_cnty %_cntx
  202.         }
  203.         if %_cnty<%_cntx {
  204.             _qqcomp _VARX %_cntx %_cnty
  205.         }
  206.         _qqalign _VARY _VARX _NEWX2
  207.         _setlbl _VARY %_1
  208.         _setlbl _NEWX2 %_2
  209.         #delimit ;
  210.         noisily graph _VARY _NEWX2 _NEWX2, %_options
  211.               ti("     Quantile-Quantile Plot") sy(oi) c(.l) sort;
  212.         #delimit cr
  213.         drop _NEWX2 _VARY _VARX
  214.         exit
  215.     }
  216.     di in red "--Break--"
  217.     capture drop _CNT
  218.     capture drop _NEWX2
  219.     capture drop _VARY
  220.     capture drop _VARX
  221. end
  222.  
  223. capture program drop qnorm
  224. program define qnorm
  225.     if "%_*"=="" {
  226.         di in bl "qnorm varname [if] [in] [, graph_options]"
  227.         exit
  228.     }
  229.     mac def _varlist "req ex max(1)"
  230.     mac def _if "opt"
  231.     mac def _in "opt"
  232.     mac def _options "*"
  233.     parse "%_*"
  234.     capture {
  235.         gen _UVAR=%_varlist %_if %_in
  236.         sort _UVAR
  237.         _nmusig2 _UVAR _CNT _MEAN _STD
  238.         if _CNT[_N]==0 {
  239.             drop _UVAR _CNT _MEAN _STD
  240.             di in blue "no observations"
  241.             exit
  242.         }
  243.         replace _STD=sqrt(_STD) in l
  244.         gen _NORM=invnorm((_n-.5)/_CNT[_N])*_STD[_N]+_MEAN[_N] /*
  245.               */  if _UVAR~=.
  246.         drop _CNT _MEAN _STD
  247.         _setlbl _UVAR %_varlist
  248.         label var _NORM "Normal"
  249.     }
  250.     if _rc {
  251.         di in red "--Break--"
  252.         capture drop _UVAR
  253.         capture drop _CNT
  254.         capture drop _MEAN
  255.         capture drop _STD
  256.         capture drop _NORM
  257.         exit
  258.     }
  259.     qqplot _UVAR _NORM, %_options
  260.     drop _NORM _UVAR
  261. end
  262.  
  263.  
  264. capture program drop grebar
  265. program define grebar
  266.     di in blue "not available in demonstration version"
  267. end
  268.  
  269. capture program drop leverage
  270. program define leverage
  271.     if "%_*"=="" {
  272.         di in blu /*
  273.        */ "->leverage lhs rhs other-rhs-vars [if] [in] [, graph_options]"
  274.         exit
  275.     }
  276.     mac def _varlist "req ex"
  277.     mac def _options "*"
  278.     mac def _in "opt"
  279.     mac def _if "opt"
  280.     parse "%_*"
  281.     parse "%_varlist", parse(" ")
  282.     mac def _lhs %_1
  283.     mac def _rhs %_2
  284.     mac shift
  285.     mac shift
  286.     capture {
  287.         reg %_lhs %_* %_if %_in, noo
  288.         predict _RESID1 %_if %_in, resid
  289.         reg %_rhs %_* %_if %_in, noo
  290.         predict _RESID2 %_if %_in, resid
  291.         reg _RESID1 _RESID2, noo
  292.         predict _PREDL %_if %_in
  293.         _setlbl _RESID1 %_lhs
  294.         _setlbl _RESID2 %_rhs
  295.         lab var _PREDL " "
  296.         #delimit ;
  297.         noisily graph _RESID1 _PREDL _RESID2, s(oi) c(.l) sort
  298.             %_options
  299.             title("     Partial Regression Leverage Plot");
  300.         #delimit cr
  301.     }
  302.     if _rc==1 {
  303.         di in red "--Break--"
  304.     }
  305.     capture drop _RESID1
  306.     capture drop _RESID2
  307.     capture drop _PREDL
  308. end
  309.  
  310.  
  311. capture program drop hilite
  312. program define hilite
  313.     if "%_*"=="" {
  314.       di in bl "->hilite yvar xvar [if] [in], hilite(exp) [graph_options]"
  315.       exit
  316.     }
  317.     mac def _varlist "req ex min(2) max(2)"
  318.     mac def _if "opt"
  319.     mac def _in "opt"
  320.     mac def _options "Hilite(string) Symbol(string) *"
  321.     parse "%_*"
  322.     if "%_hilite"=="" {
  323.         hilite
  324.     }
  325.     capture _chkstr if %_hilite
  326.     if _rc {
  327.         exit
  328.     }
  329.     parse "%_varlist", parse(" ")
  330.     if "%_symbol"=="" {
  331.         mac def _symbol ".o"
  332.     }
  333.     capture {
  334.         gen _Temp = %_1 if %_hilite  %_in
  335.         mac def _w : variable label %_1
  336.         if "%_w"=="" {
  337.             mac def _w "%_1"
  338.         }
  339.         capture noisily graph %_1 _Temp %_2 %_if %_in, s(%_symbol) /*
  340.               */ t1("%_hilite highlighted") l1("%_w") %_options
  341.         capture {
  342.             drop _Temp
  343.             exit
  344.         }
  345.     }
  346.     di in red "--Break--"
  347.     capture drop _Temp
  348. end
  349.  
  350.  
  351.  
  352. di
  353. di in gr "The following new commands are now available:"
  354. di "   grebar         leverage       qqplot         quantile      symplot"
  355. di "   hilite         qnorm"
  356.  
  357.  
  358. di _n in gr "See " in wh "help graph.kit" in gr "."
  359. exit
  360.  
  361. Notes to User's wishing to edit this file:
  362.  
  363. These comments are placed at the end of the file, after the exit, so that
  364. Stata does not have to read through them every time the user types "run
  365. Graph.Kit"
  366.  
  367. Feel free to edit this file and delete programs in which you have no interest.
  368. Before editing this file, however, make sure the the original is BACKED UP.
  369. Please do not remove the copyright notice at the top of this file.
  370.  
  371. If you do edit the file, you should add a display command at the top of this
  372. file indicating that you have edited the file.  You should change the display
  373. just above these comments that lists the new commands now available.
  374.  
  375. All utility programs start with an underscore '_'.
  376.  
  377. Some programs use other programs in achieving the desired result.  Review the
  378. table below carefully before deleting programs.
  379.  
  380.         Program         Requires
  381.         --------------------------
  382.         grebar
  383.         hilite          _chkstr
  384.         leverage        _setlbl
  385.         qnorm           _setlbl  _nmusig2  qqplot
  386.         qqplot          _setlbl  _qqalign  _qqcomp
  387.         quantile        _setlbl
  388.         symplot
  389.  
  390. For instance, if you deleted the _setlbl program, you would break leverage,
  391. qnorm, qqplot, and quantile.
  392.  
  393. <end of Graph.Kit>
  394.